package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"awesomeProject2/model/request"
	"awesomeProject2/model/response"
	"awesomeProject2/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func CreateTeacher(c *gin.Context) {
	var teacher model.Teacher
	var data request.TeacherInfo

	_ = c.ShouldBindJSON(&data)

	service.FillTeacher(&teacher, &data)
	if err := model.CreateTeacher(teacher); err != nil {
		response.FailWithError(err, c)
	} else {
		response.Ok(c)
	}
}

func GetTeacher(c *gin.Context) {
	var l request.Id

	if err := c.ShouldBindUri(&l); err != nil {
		response.FailWithError(err, c)
		return
	}
	if err, data := model.GetTeacherById(l.Id); err != nil {
		response.FailWithError(err, c)
	} else {
		response.OkWithData(data, c)
	}
}

func GetTeacherInfo(c *gin.Context) {
	if err, data := model.GetCurrentTeacher(c); err != nil {
		response.FailWithError(err, c)
	} else {
		response.OkWithData(data, c)
	}
}

func UpdateTeacher(c *gin.Context) {
	var teacher model.Teacher
	var data request.TeacherInfo

	_ = c.ShouldBindJSON(&data)
	service.FillTeacher(&teacher, &data)

	if err := service.UpdateTeacher(c, &teacher); err != nil {
		response.FailWithError(err, c)
	} else {
		response.Ok(c)
	}
}
func DeleteTeacher(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.DeleteTeacher(id); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "删除成功", "status": http.StatusOK})
	}
}

//GetPaperToshow
func GetPaperToShow(c *gin.Context) {
	userid, _ := c.Get("userid")
	if err, papers, total := model.GetPaperToShow(userid.(uint)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "获取失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  papers,
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func GetPaperToShowAdmin(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var tc model.Teacher
	global.GVA_DB.Model(&model.Teacher{}).Where("id = ?", uint(id)).First(&tc)
	userid := tc.UserId

	if err, papers, total := model.GetPaperToShow(userid); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "获取失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  papers,
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func GetProjectToShow(c *gin.Context) {
	userid, _ := c.Get("userid")
	if err, projects, total := model.GetProjectToShow(userid.(uint)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "查询失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  projects,
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func GetProjectToShowAdmin(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var tc model.Teacher
	global.GVA_DB.Model(&model.Teacher{}).Where("id = ?", uint(id)).First(&tc)
	userid := tc.UserId

	if err, projects, total := model.GetProjectToShow(userid); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "查询失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  projects,
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func GetWorkToShow(c *gin.Context) {

	userid, _ := c.Get("userid")
	if err, works, total := model.GetWorkToShow(userid.(uint)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  works,
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func GetPatentToShow(c *gin.Context) {
	userid, _ := c.Get("userid")

	if err, datas, total := model.GetPatentToShow(userid.(uint)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  datas,
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func GetAchievementToShow(c *gin.Context) {
	userid, _ := c.Get("userid")
	if err, datas, total := model.GetAchievementToShow(userid.(uint)); err != nil {
		response.Fail(c)
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  datas,
			"tatal": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func GetAllTeachers(c *gin.Context) {
	if err, data := model.GetAllTeachers(); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "获取失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"teacherList": data},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		},
		)
	}
}
func GetAllInfoAdmin(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var tc model.Teacher
	global.GVA_DB.Model(&model.Teacher{}).Where("id = ?", uint(id)).First(&tc)
	userid := tc.UserId
	var paper, project, patent, work, ac int64
	_, _, paper = model.GetAllPapers3(userid)
	_, _, project = model.GetAllProjects3(userid)
	_, _, patent = model.GetAllPatens3(userid)
	_, _, work = model.GetAllWorks3(userid)
	_, _, ac = model.GetAllAchievements(userid)
	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{"paper": paper, "project": project, "patent": patent, "work": work, "achievement": ac},
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}

//获取今年的成果
func GetAllInfo(c *gin.Context) {
	user_id, _ := c.Get("userid")
	userid := user_id.(uint)
	var paper, project, patent, work, ac int64
	_, _, paper = model.GetAllPapers3(userid)
	_, _, project = service.GetAllProjectsbq(userid, model.GetYear())
	_, _, patent = model.GetAllPatens3(userid)
	_, _, work = model.GetAllWorks3(userid)
	_, _, ac = model.GetAllAchievements(userid)
	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{"paper": paper, "project": project, "patent": patent, "work": work, "achievement": ac},
		"meta": gin.H{
			"msg":    "查询成功",
			"status": http.StatusOK,
		},
	})
}

//管理员功能,设置年份

func UpadateYear(c *gin.Context) {
	year := c.Param("year")
	data, _ := strconv.Atoi(year)
	err := model.UpdateConfig(data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "修改失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"meta": gin.H{
				"msg":    "更新成功",
				"status": http.StatusOK,
			},
		})
	}
}
